<style lang="less">
@import '../../assets/style/jinjiqiuzhu.less';
</style>

<template>
    <mxq-router-layout>
        <div class="sos">
            <Head slot="header" @headerClick="headerClick($event)" :isIconShow="isIconShow">
                <div slot="headerTitle">
                    紧急求助
                </div>
            </Head>

            <van-cell-group>
                <van-cell v-for="(item,index) in helpList" :key="index">
                    <img slot="icon" src="../../assets/img/jinjiqiuzhu/icon_sos.png" class="img_l">
                    <van-tag  slot="icon" type="danger" v-if="item.NUM==0" v-show="isNum">{{item.num}}</van-tag>
                    <van-tag  slot="icon" type="danger" v-else>{{item.num}}</van-tag>
                    <img src="../../assets/img/jinjiqiuzhu/icon_send.png" alt="" class="img_r" @click="sendHelp(item)" />
                    <!--<span v-if="item.flag==0">
                        <van-tag type="danger">等待求助</van-tag>
                    </span>
                    <span  v-else>
                        <van-tag type="success">已解决</van-tag>
                    </span>-->
                    <template slot="title">
                        <span class="van-cell-text">{{item.name}}</span>
                        <div class="nums">
                            <span>{{item.helpCont}}</span>
                        </div>
                    </template>
                </van-cell>
            </van-cell-group>
        </div>
    </mxq-router-layout>
</template>
<script>
import Head from '@/components/head'
import bus from "../../libs/bus.js"
import { getLocation } from '../../api/android'
import { callData } from '../../axios/axios'
import {
    Cell,
    CellGroup,
    Tag,
    Toast,
    Dialog
} from 'vant'

export default {
    name: 'news',
    components: {
        Head,
        [Cell.name]: Cell,
        [CellGroup.name]: CellGroup,
        [Tag.name]: Tag,
        [Toast.name]: Toast
    },
    data() {
        return {
        		isIconShow: false,//是否显示左侧箭头图标
            helpList:[], //列表数据
           	jdwdMeg:{//非安卓环境下参数
	            	lon:'',
	            	lat:'',
	            	address:''
            },
            lon: '',    //安卓经度
            lat: '',    //安卓纬度
            isNum: false,
        }
    },
    mounted(){
    		this.getHelpList()
    },
    methods: {
		// 头部两侧按钮点击事件
		headerClick(e){
			bus.$emit('actives',2)
		},
		//获取紧急求助列表
		getHelpList(){
			callData('/statisServer/CmAlarm/findCmAlarmApp','get',{})
			.then(res => {
				if(res.success){
					var helpDatas = res.data;
					helpDatas.forEach((item,index) => {
						item.name = item.TYPE_NAME;
						item.helpCont = item.TYPE_DETAILS;
						item.num = item.NUM;
					})
					this.helpList = helpDatas;
				}
			})
		},
        // 发送求助
        sendHelp(data){
        		console.log('data',data);
        		Dialog.confirm({
                title: '紧急求助',
                message: '是否发送紧急求助？'
            }).then(() => {
                // on confirm
				if(window.Cordova){
                    getLocation(res => {
                    		var params = {};
                        params.jd = res.data.lon;
                        params.wd = res.data.lat;
						params.alarmMessageTypeId = data.TYPE; //列表ID
                        params.alarmAddress=res.data.address;  // 当前的地理位置
                        console.log('告警jd',res.data.lon);
                        console.log('告警wd',res.data.lat);
                        console.log('告警地址',res.data.address);
                        callData('/statisServer/CmAlarm/saveCmAlarm', 'post', params).then(res => {
                            if(res.success){
                            		Toast('告警发送成功');
                            		this.getHelpList();
                            }
                            else {
                            		Toast('告警发送失败');
                        		}
                        })
                    })
                }
				else{
                    var params = {};
                    params.jd = this.jdwdMeg.lon;            //经度 
                    params.wd = this.jdwdMeg.lat;            //纬度
					params.alarmMessageTypeId = data.TYPE;	//列表ID
                    params.alarmAddress = this.jdwdMeg.address;
                    callData('/statisServer/CmAlarm/saveCmAlarm', 'post', params).then(res => {
                        if(res.success){
                        		Toast('告警发送成功');
                        		this.getHelpList();
                        }
                        else {
                            Toast('告警发送失败');
                        }
                    })
                }
            }).catch(() => {
                // on cancel
            });
        }
	}
}
</script>
